#include "camerausb.h"

CameraUsb& CameraUsb::getInstance()
{
    static CameraUsb instance;
    return instance;
}

CameraUsb::~CameraUsb()
{

}

bool CameraUsb::getIsOpened()
{
    return cap.isOpened();
}

void CameraUsb::setExposure(int exposure) {
    LogInfo << "goin  setExposure empty" << exposure;
    return;
}

CameraUsb::CameraUsb(QObject *parent)
{

}

void CameraUsb::capRelease() {
    if (cap.isOpened()) {
        cap.release();
    }
}

void CameraUsb::capOpen() {
    std::string pipeline = "v4l2src device=/dev/video0 ! image/jpeg, width=1920, height=1080, framerate=30/1 ! jpegdec ! videoconvert ! appsink";
    //cap.open(pipeline, cv::CAP_GSTREAMER);
    cap.open(0);  // 0 默认打开第一个摄像头

}
cv::Mat CameraUsb::getImage()
{
    cv::Mat frame;
    try {
        if (cap.read(frame)) {
            LogDebug << "Camera successfully captured a frame.";
        } else {
            LogError << "Camera failed to capture";
            setStatus(Error,"Failed to capture frame");
        }
    } catch (const cv::Exception& e) {
        LogError << "OpenCV exception on frame capture: " << e.what();
        setStatus(Error,"OpenCV exception occurred");
    } catch (const std::exception& e) {
        LogError << "Standard exception on frame capture: " << e.what();
        setStatus(Error,"Standard exception occurred");
    } catch (...) {
        LogError << "Unknown exception on frame capture.";
        setStatus(Error,"Unknown exception occurred");
    }
    return frame;
}
